﻿@using CuongHanh
@model int
@{
    ViewBag.Title = "SubCategory";
    Layout = "~/Views/Shared/_ALayout.cshtml";

}
@section script
{
    <script type="text/javascript">
        function getImage(value, record) {
            var url = '@Url.Content("~/Content/Images/Thumnail/")';
            return "<img alt='no image' src='" + url + value + "'/>";
        }

        function handlerCommand(command, record) {

            if (command == 'View') {
                var width = parseFloat('@AppUtil.WIDTH_LAGRE');
                var height = parseFloat('@AppUtil.HEIGHT_LAGRE');
                if (width > height) {
                    if (width > 500) {
                        height = (height / width) * 500;
                        width = 500;
                    }
                } else if (height > 500) {
                    width = (width / height) * 500;
                    height = 500;
                }

                var url = '@Url.Content(AppUtil.SMALL_IMAGE_PATH)';
                $('#wdImage').attr('src', '');
                $('#wdImage').width(width);
                $('#wdImage').height(height);
                $('#wdImage').attr('src', url + record.data.Image);
                window.App.wdModify.setHeight(height);
                window.App.wdModify.setWidth(width);
                window.App.wdModify.center();
                window.App.wdModify.show();
            }
        }
    </script>
}


<form id="fileUpload" enctype="multipart/form-data">
    @(Html.X()
              .TabPanel()
              .Margin(5)
              .Items(tab =>
                     tab.Add(Html.X()
                                     .Panel()
                                     .Layout(LayoutType.Border)
                                     .Title("Hình ảnh")
                                     .Border(false)
                                     .PaddingSpec("0px 0px 0px 0px")
                                     .Height(500)
                                     .Items(
                                            view =>
                                                {
                                                    Toolbar topMenu = new Toolbar
                                                                          {
                                                                                  Region = Region.North
                                                                          };
                                                    view.Add(topMenu);
                                                    topMenu.Add(Html.X()
                                                                        .Label()
                                                                        .Text("Thêm ảnh: "));
                                                    topMenu.Add(Html.X().FormPanel()
                                                                        .Layout(LayoutType.Column)
                                                                        .ID("BasicForm")
                                                                        .Frame(true)
                                                                        .Padding(0)
                                                                        .Margin(0)
                                                                        .Items(
                                                                               Html.X().FileUploadField()
                                                                                       .ID("FileUploadField1")
                                                                                       .Width(300)
                                                                                       .EmptyText("chọn file ảnh upload")
                                                                                       .ButtonText("")
                                                                                       .Icon(Icon.ImageAdd)
                                                                               ,
                                                                               Html.X().Button()
                                                                                       .ID("SaveButton")
                                                                                       .Text("Lưu lại")
                                                                                       .Disable(true)
                                                                                       .DirectEvents(de =>
                                                                                                         {
                                                                                                             de.Click.Url = Url.Action("UploadClick");
                                                                                                             de.Click.ExtraParams.Add(new Parameter("productId",
                                                                                                                                                    Model + ""));
                                                                                                             de.Click.FormID = "fileUpload";
                                                                                                             de.Click.Before = @"if (!#{BasicForm}.getForm().isValid()) { return false; } Ext.Msg.wait('Uploading your photo...', 'Uploading');";
                                                                                                             de.Click.Failure = @"Ext.Msg.show({ 
                                                                                                                                            title   : 'Error', 
                                                                                                                                            msg     : 'Error during uploading', 
                                                                                                                                            minWidth: 200, 
                                                                                                                                            modal   : true, 
                                                                                                                                            icon    : Ext.Msg.ERROR, 
                                                                                                                                            buttons : Ext.Msg.OK 
                                                                                                                                        });";
                                                                                                         })
                                                                        )
                                                            );
                                                    topMenu.Add(new ToolbarFill());
                                                    view.Add(Html.X()
                                                                     .GridPanel()
                                                                     .ID("gvData")
                                                                     .View(Html.X()
                                                                                   .GridView()
                                                                                   .EnableTextSelection(true)
                                                                     )
                                                                     .Region(Region.Center)
                                                                     .ColumnModel(column =>
                                                                                      {
                                                                                          column.Add(Html.X()
                                                                                                             .Column()
                                                                                                             .DataIndex("Image")
                                                                                                             .Text("Hình")
                                                                                                             .Renderer("getImage")
                                                                                                  );
                                                                                          column.Add(Html.X()
                                                                                                             .Column()
                                                                                                             .DataIndex("Image")
                                                                                                             .Text("Tên")
                                                                                                             .Width(300)
                                                                                                  );
                                                                                          column.Add(Html.X()
                                                                                                             .CheckColumn()
                                                                                                             .DataIndex("IsDefault")
                                                                                                             .Text("Hình chính")
                                                                                                  );
                                                                                          column.Add(Html.X()
                                                                                                             .CommandColumn()
                                                                                                             .Commands(v => v.Add(new GridCommand
                                                                                                                                      {
                                                                                                                                              Icon = Icon.Image,
                                                                                                                                              CommandName = "View",
                                                                                                                                              Text = "Xem ảnh"
                                                                                                                                      })
                                                                                                             )
                                                                                                             .Listeners(c =>
                                                                                                                            {
                                                                                                                                c.Command.Handler = "handlerCommand(command, record);";
                                                                                                                            })
                                                                                                  );
                                                                                          column.Add(Html.X()
                                                                                                             .CommandColumn()
                                                                                                             .PaddingSpec("0 10")
                                                                                                             .Align(Alignment.Center)
                                                                                                             .MinWidth(150)
                                                                                                             .MaxWidth(150)
                                                                                                             .Commands(v =>
                                                                                                                       v.Add(new GridCommand
                                                                                                                                 {
                                                                                                                                         Icon = Icon.ApplicationOsxKey,
                                                                                                                                         CommandName = "SetImageIsDefault",
                                                                                                                                         Text = "Chọn làm ảnh chính"
                                                                                                                                 })
                                                                                                             )
                                                                                                             .DirectEvents(de =>
                                                                                                                               {
                                                                                                                                   de.Command.EventMask.ShowMask = true;
                                                                                                                                   de.Command.ExtraParams.Add(new Parameter("command",
                                                                                                                                                                            "command",
                                                                                                                                                                            ParameterMode.Raw));
                                                                                                                                   de.Command.ExtraParams.Add(new Parameter("id",
                                                                                                                                                                            "record.data.Id",
                                                                                                                                                                            ParameterMode.Raw));
                                                                                                                                   de.Command.Url = Url.Action("Command");
                                                                                                                                   de.Command.Confirmation.ConfirmRequest = true;
                                                                                                                                   de.Command.Confirmation.BeforeConfirm = "config.confirmation.title = 'Thông báo'; config.confirmation.message = 'Bạn có muốn chọn ảnh này làm ảnh chính cho sản phẩm?';";
                                                                                                                                   de.AutoDataBind = true;
                                                                                                                               })
                                                                                                  );

                                                                                          column.Add(Html.X()
                                                                                                             .CommandColumn()
                                                                                                             .Width(120)
                                                                                                             .PaddingSpec("0 10")
                                                                                                             .Align(Alignment.Center)
                                                                                                             .Commands(v =>
                                                                                                                       v.Add(new GridCommand
                                                                                                                                 {
                                                                                                                                         Icon = Icon.Delete,
                                                                                                                                         CommandName = "DeleteProductImage",
                                                                                                                                         Text = "Xóa"
                                                                                                                                 })
                                                                                                             )
                                                                                                             .DirectEvents(de =>
                                                                                                                               {
                                                                                                                                   de.Command.EventMask.ShowMask = true;
                                                                                                                                   de.Command.ExtraParams.Add(new Parameter("command",
                                                                                                                                                                            "command",
                                                                                                                                                                            ParameterMode.Raw));
                                                                                                                                   de.Command.ExtraParams.Add(new Parameter("id",
                                                                                                                                                                            "record.data.Id",
                                                                                                                                                                            ParameterMode.Raw));
                                                                                                                                   de.Command.Url = Url.Action("Command");
                                                                                                                                   de.Command.Confirmation.ConfirmRequest = true;
                                                                                                                                   de.Command.Confirmation.BeforeConfirm = "config.confirmation.title = 'Message'; config.confirmation.message = 'Bạn có muốn xóa ảnh này không?';";
                                                                                                                                   de.AutoDataBind = true;
                                                                                                                               })
                                                                                                  );
                                                                                      })
                                                                     .Store(Html.X()
                                                                                    .Store()
                                                                                    .RemotePaging(true)
                                                                                    .PageSize(30)
                                                                                    .ID("storeGvData")
                                                                                    .AutoLoad(true)
                                                                                    .AutoSync(false)
                                                                                    .Model(c => c.Add(Html.X()
                                                                                                              .Model()
                                                                                                              .Fields("Id",
                                                                                                                      "Image",
                                                                                                                      "IsDefault"
                                                                                                              )
                                                                                                              .IDProperty("Id")))
                                                                                    .Proxy(c => c.Add(Html.X()
                                                                                                              .AjaxProxy()
                                                                                                              .Json(true)
                                                                                                              .Url(Url.Action("GetImagesByProduct"))
                                                                                                              .ActionMethods(v => v.Read = HttpMethod.POST)
                                                                                                              .Reader(Html.X()
                                                                                                                              .JsonReader()
                                                                                                                              .Root("data")
                                                                                                                              .TotalProperty("total"))
                                                                                                              .ExtraParams(param => param.Add(new Parameter("productId",
                                                                                                                                                            Model + "",
                                                                                                                                                            ParameterMode.Raw))
                                                                                                              )
                                                                                                        ))
                                                                     )
                                                            );
                                                }))
              ))
</form>



@(Html.X()
          .Window()
          .ID("wdModify")
          .Hidden(true)
          .Modal(true)
          .Resizable(false)
          .Title("View")
          .Width(600)
          .Height(600)
          .Constrain(true)
          .StyleHtmlCls("background-color:white;")
          .Html("<img id='wdImage' alt='no image' style='width:600px;' src=''/>")
          )